/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-09-15
 * Time: 20:29
 */
public class Hashbuck {
    //数组+链表
    static class Node{
        int key;
        int val;
        Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    //
    public static final int DEFAULT_CAPACITY = 10;
    //Node类型的数组
    Node[] arr = new Node[DEFAULT_CAPACITY];
    int usedsize = 0;

    /**
     * 插入
     */
    public void put(int key,int val){
        //将要插入的值转化成一个节点
        Node node = new Node(key,val);
        //利用哈希函数找到key放的位置
        int index = key%arr.length;
        //每个节点又是一个链表,所以遍历这个链表:用cur来记录index处的node
        Node cur  = arr[index];
        //如果链表里有与node.key一样的节点,直接修改val
        while (cur != null){
            if(cur.key == node.key ){
                cur.val = node.val;
                return;
            }
            cur = cur.next;
        }
        //代码走到这里,链表里没有node这个节点,直接头插
        node.next = arr[index];
        arr[index] = node;
        usedsize++;
    }
}
